Encoding Function

Returns the text encoding of the passed String.

Syntax

result = Encoding( str )

result = str.Encoding


Parameters

str

String

The String whose TextEncoding will be returned.



Notes

Use the Encoding function to determine the encoding of a String. This is useful for text that you have read in from an external source, for example, with TextInputStream and you don't know the encoding. For text that is generated within REALbasic, the encoding is known to REALbasic.

If the string's encoding is unknown, Encoding returns Nil. Test whether the TextEncoding object is Nil or include an Exception block if there is a chance the string's encoding would not be known at runtime.


Example

The following example gets the encoding of the text stored in the local variable, s. The Exception block handles the call to one of the TextEncoding object's properties if the encoding cannot be determined or the file cannot be opened.

Dim f As FolderItem
Dim t As TextInputStream
Dim s As String
Dim enc as TextEncoding
f= GetOpenFolderItem("text") //file type defined via the FileType class
 t=f.OpenAsTextFile
 s=t.ReadAll
 t.Close
enc=s.Encoding
Exception err as NilObjectException
  MsgBox err.message

See Also

TextConverter, TextEncoding classes; ConvertEncoding, DefineEncoding, GetTextEncoding functions; Encodings object.